# coding: utf-8
from Tkinter import *
from ttk import *

from utils.FileOperator import FileOperator


class FileEditor:  # 定义编辑框类
    def __init__(self, root, file_name):  # 初始化初始值
        self.top = Toplevel(root)  # 生成Toplevel组件
        self.textPad = Text(self.top, width=80, height=24)
        self.fp = FileOperator(file_name)

        self.create_widgets()

    def create_widgets(self):
        """拖控件"""
        self.frame_btn = Frame(self.top)
        self.frame_btn.pack(side=TOP, anchor=W)
        self.btn_save = Button(self.frame_btn, text="Save", command=self.save)
        self.btn_save.pack(side=LEFT)
        self.btn_refresh = Button(self.frame_btn, text="Refresh", command=self.load)
        self.btn_refresh.pack(side=LEFT)
        self.load()
        scrl = Scrollbar(self.top)
        scrl.pack(side=RIGHT, fill=Y)
        self.textPad.configure(yscrollcommand=scrl.set)
        self.textPad.pack(side=TOP, fill=BOTH, expand=True)
        scrl['command'] = self.textPad.yview

    def save(self):  # 定义按钮事件处理函数
        self.fp.set_ini(self.textPad.get(1.0, "end"))
        self.fp.save()

    def load(self):  # 定义按钮事件处理函数
        """取消返回处理"""
        self.fp.load()
        self.textPad.delete(1.0, "end")
        self.textPad.insert(1.0, self.fp.get_ini())


class LabelEntry:
    def __init__(self, parent, label, textvariable, entry="", width=5):
        self.parent = parent
        self.textvariable = textvariable
        self.content = StringVar()
        self.content.set(entry)

        self.frame = Frame(self.parent, height=120)

        Label(self.frame, text=label+" : ", width=10).pack(side=LEFT)
        Entry(self.frame, textvariable=textvariable, width=10).pack(side=LEFT)
        self.frame.pack()

    def get_entry(self):
        return self.content.get()


class LabelSpinbox:
    def __init__(self, parent, label, textvariable, entry="", width=10):
        self.parent = parent
        self.textvariable = textvariable
        self.content = StringVar()
        self.content.set(entry)

        self.frame = Frame(self.parent, height=120)

        Label(self.frame, text=label+" : ", width=10).pack(side=LEFT)
        Spinbox(self.frame, textvariable=textvariable, width=9, increment=10000, from_=100, to=1000000).pack(side=LEFT)
        self.frame.pack()

    def get_entry(self):
        return self.content.get()


class ApkItem:
    def __init__(self, parent, apk_info):
        self.parent = parent
        self.apk_info = apk_info
        self.b_var = BooleanVar

        self.frame = Frame(self.parent)

        Radiobutton(self.frame, state=self.b_var).pack(side=LEFT)
        Label(self.frame, text=apk_info["versionName"]+" : ", padding=10).pack(side=LEFT)
        self.frame.pack()

    def get_testApkUrl(self):
        return self.apk_info["testApkUrl"]

    def get_releaseApkUrl(self):
        return self.apk_info["releaseApkUrl"]
